/**
 * Created by lxc on 15-12-17.
 */
var util = require('util');

var auth = require('../../helpers/auth.js');
var db = require('../../models/index.js');
var render = require('../../instances/render.js');
var debug = require('../../instances/debug.js');


module.exports = (router) => {

    var Admins = db.models.Adminer;

    router.get('/adminer-super/accountlist', function *() {
        var admin = yield Admins.findAll({
            where: {
                status: 0
            },
            include:[{
                model:db.models.shops,
                attributes:['title','id']
            }]
        });
        this.body = yield render('admin/AccountList', {
            admin
        });
    });

    router.post('/adminer-super/accountadd', function *() {
        var body = this.request.body;
        debug(body);

        var nickname = body.nickname;
        var shopExsit = yield db.models.shops.findOne({
            where:{
                id:body.shopId
            }
        });
        if(!shopExsit){
            this.body = -1;
            return;
        }
        ///修改
        if (body.id != "") {
            var is = yield Admins.findOne({
                where: {
                    nickname: nickname,
                    id: {
                        $ne: body.id
                    },
                    shopId:body.shopId
                }
            });
            if (is == null) {
                console.log("修改");
                var th = yield Admins.findOne({
                    where: {
                        id: body.id
                    }
                });

                th.nickname = body.nickname;
                th.name = body.name;
                th.phone = body.phone;
                th.password = body.password;
                th.shopId = body.shopId;
                if (th.type != 100 && body.type != 100) {
                    th.type = body.type;
                }
                yield th.save();
                this.body = th.id
            } else {
                console.log("不修改");
                this.body = -1
            }
        } else {
            ///添加
            is = yield Admins.findOne({
                where: {
                    nickname: nickname
                }
            });
            debug(is);
            if (is == null) {
                var e = yield Admins.create({
                    nickname: body.nickname,
                    type: body.type,
                    name: body.name,
                    phone: body.phone,
                    password: body.password,
                    status: 0,
                    shopId: body.shopId
                });
                this.body = e.id
            } else {
                this.body = -1;
            }
        }


    });


    router.get('/adminer-super/getaccount', function *() {

        var id = this.query.id;
        var is = yield Admins.findOne({
            where: {
                id: id
            },
            include:[{
                model:db.models.shops,
                attributes:['title']
            }]
        });
        this.body = JSON.stringify(is);
    });

    router.get('/adminer-super/delaccount', function *() {

        var id = this.query.id;
        var is = yield Admins.findOne({
            where: {
                id: id
            }
        });
        is.destroy();
        yield is.save();
        this.body = 1;
    });

};